package com.lijie.bs_recruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.positionType;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Company;
import com.lijie.bs_recruit.model.Position;
import com.lijie.bs_recruit.service.CompanyService;
import com.lijie.bs_recruit.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/position")
public class PositionController {

    @Autowired
    PositionService  positionService;
    @Autowired
    CompanyService companyService;

    @Autowired
    positionType pType;

    //分页查询，按条件查询
    @RequestMapping("/selectPosition/{page}")
    public PageInfo<Position> selectPosition(@PathVariable Integer page, @RequestBody Position position){
        PageInfo<Position> positions = positionService.selPositionPage(page, position);
        return positions;
    }

    //管理员查看已经审核过的（包含审核通过和为通过）
    @RequestMapping("/adminSelPositionCheck/{page}")
    public PageInfo<Position> adminSelPositionCheck(@PathVariable Integer page, @RequestBody Position position){
        PageInfo<Position> positions = positionService.adminSelPositionChecked(page, position);
        return positions;
    }



    //hr查看已上线的职位 check:1  、deleted:0 未删除的 、isopen 1 上线的
    @RequestMapping("/selectPositionByhr/{page}")
    public PageInfo<Position> selectPositionByhr(@PathVariable Integer page, @RequestBody Position position){
        PageInfo<Position> positions=new PageInfo<>();
        if(position.getHrId()!=null){
            position.setIscheck(pType.checked);
            position.setIsdeleted(pType.noDeleted);
            position.setIsOpen(pType.open);
            positions = positionService.selPositionPage(page, position);
        }
        return positions;
    }

    //hr更改职位上架状态  0下架  1上架  （数据库默认）
    @RequestMapping("/updatePositionOpen")
    public Boolean updatePositionOpen(@RequestBody Position position){
        Boolean aBoolean = positionService.updatePositionOpen(position.getPositionId());
        return aBoolean;
    }
    //获得单个职位的详细信息
    @RequestMapping("/selPositionById")
    public ResultVO<Position> selPositionById(@RequestBody Position position){
        ResultVO<Position> resultVO=new ResultVO<>();
        Position positionResult = positionService.selPosotionByid(position.getPositionId());
        if(positionResult!=null){
            resultVO.setMessage("succ");
            resultVO.setObject(positionResult);
        }else {
            resultVO.setMessage("error");
        }
        return resultVO;
    }

    //hr增加职位
    @RequestMapping("/addPosition")
    public String addPosition(@RequestBody Position position){
        String result="";
        //获得hrId所在的公司id
        Company company = companyService.selCompanyByhrId(position.getHrId());
        position.setCompanyId(company.getCompanyId());
        position.setCompanyName(company.getCompanyName());
        boolean save = positionService.save(position);
        if(save){
            result="succ";
        }else{
            result="error";
        }
        return result;
    }

    //hr更改职位信息
    @RequestMapping("/updatePosition")
    public String updatePosition(@RequestBody Position positoin){
        String result="";
        positoin.setCreateTime(null);//去除生成时间
        positoin.setUpdateTime(null);//去除上次更新时间
        UpdateWrapper<Position> wrapper=new UpdateWrapper<>();
        wrapper.eq("positionId",positoin.getPositionId());
        boolean update = positionService.update(positoin, wrapper);
        if(update){
            result="succ";
        }else{
            result="error";
        }
        return result;
    }

    //hr查看已经下线的职位
    @RequestMapping("/selNoOpenPosition/{page}")
    public PageInfo<Position> selNoOpenPosition(@PathVariable Integer page, @RequestBody Position position){
        PageInfo<Position> positions=new PageInfo<>();
        if(position.getHrId()!=null){
            position.setIscheck(pType.checked);
            position.setIsdeleted(pType.noDeleted);
            position.setIsOpen(pType.noOpen);
            positions = positionService.selPositionPage(page, position);
        }
        return positions;
    }

    //hr查看未通过审核(未审核和审核失败)的职位
    @RequestMapping("/selNoCheckedPosition/{page}")
    public PageInfo<Position> selNocheckPosition(@PathVariable Integer page, @RequestBody Position position){
        PageInfo<Position> positions=new PageInfo<>();
        if(position.getHrId()!=null){
            QueryWrapper<Position> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("hrId", position.getHrId())
                    .eq("isdeleted",pType.noDeleted)
                    .eq("isOpen", pType.open)
                    .ne("ischeck",pType.checked);
            positions = positionService.selPositionByWrapper(page, queryWrapper);
        }
        return positions;
    }

    //hr删除职位
    @RequestMapping("/deletePosition/{positionId}")
    public Boolean deletePosition(@PathVariable Integer positionId){
        UpdateWrapper<Position> wrapper=new UpdateWrapper<>();
        wrapper.set("isdeleted", 1)
                .eq("positionId", positionId);
        Boolean update = positionService.update(wrapper);
        return update;
    }

    //公司下正在招聘的职位
    @RequestMapping("/selPositionByComId/{pageIndex}")
    public PageInfo<Position> selPositionByComId(@PathVariable Integer pageIndex, @RequestBody Position position){
        PageInfo<Position> positions=new PageInfo<>();
        if(position.getCompanyId()!=null){
            QueryWrapper<Position> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("companyId", position.getCompanyId())
                    .eq("isdeleted",pType.noDeleted)
                    .eq("isOpen", pType.open)
                    .eq("ischeck",pType.checked);//不等于1
            positions = positionService.selPositionByWrapper(pageIndex, queryWrapper);
        }
        return positions;
    }



}
